package demo5;

public class Test {

    //多态的好处
    //1.降低代码的”圈复杂度“，避免使用大量的if-else
    //2.可扩展能力强

    public static void main(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();

        Shape[] shapes={cycle,cycle,triangle,rect};
        for (Shape shape:shapes) {
            shape.draw();
        }
    }
    public static void main2(String[] args) {
        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();

        String[] strings={"cycle","rect","triangle","cycle","cycle"};

        for (String s:strings) {
            if (s.equals("cycle")){
                cycle.draw();
            }else if(s.equals("rect")){
                rect.draw();
            }else{
                triangle.draw();
            }
        }
    }

    //由于Cycle Rect Triangle都继承了Shape，并且分别都重写了draw方法
    //所以程序运行的时候会调用子类的draw方法，而不调用父类的draw方法
    public static void drawMap(Shape shape){
        shape.draw();
    }
    public static void main1(String[] args) {

        Rect rect=new Rect();
        Cycle cycle=new Cycle();
        Triangle triangle=new Triangle();

        drawMap(rect);
        drawMap(cycle);
        drawMap(triangle);

    }
}
